package com.limh.readers.utils

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern
import android.util.TypedValue
import android.telephony.TelephonyManager


/**
 * Function:
 * author: limh
 * time:2017/7/24
 */
object Utils {
    val df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CANADA)
    val dfDate = SimpleDateFormat("yyyy-MM-dd", Locale.CANADA)

    fun getTime(): String {
        return df.format(System.currentTimeMillis())
    }

    fun getDate(): String {
        return dfDate.format(System.currentTimeMillis())
    }

    /**
     * 验证邮箱
     */
    fun isEmail(str: String): Boolean {
        val rex = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"
        return Pattern.matches(rex, str)
    }

    /**
     * 验证手机号
     */
    fun isPhone(str: String): Boolean {
        val rex = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$"
        return Pattern.matches(rex, str)
    }

    fun dip2px(context: Context, dpValue: Float): Int {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.resources.getDisplayMetrics()).toInt()
    }

    val NETWORK_NONE = 0
    val NETWORK_WIFI = 1
    val NETWORK_2G = 2
    val NETWORK_3G = 3
    val NETWORK_4G = 4
    val NETWORK_MOBILE = 5

    fun getNetworkState(context: Context): Int {
        val connManager: ConnectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE)
                as ConnectivityManager
        val netInfo = connManager.activeNetworkInfo
        if (null == netInfo || !netInfo.isAvailable)
            return NETWORK_NONE
        val wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
        if (null != wifiInfo) {
            val state = wifiInfo.state
            if (null != state)
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING)
                    return NETWORK_WIFI
        }
        // 如果不是wifi，则判断当前连接的是运营商的哪种网络2g、3g、4g等
        val networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
        if (null != networkInfo) {
            val state = networkInfo.state
            val strSubTypeName = networkInfo.subtypeName
            if (null != state)
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    when (netInfo.getSubtype()) {
                    //如果是2g类型
                        TelephonyManager.NETWORK_TYPE_GPRS, // 联通2g
                        TelephonyManager.NETWORK_TYPE_CDMA, // 电信2g
                        TelephonyManager.NETWORK_TYPE_EDGE, // 移动2g
                        TelephonyManager.NETWORK_TYPE_1xRTT,
                        TelephonyManager.NETWORK_TYPE_IDEN -> return NETWORK_2G
                    //如果是3g类型
                        TelephonyManager.NETWORK_TYPE_EVDO_A, // 电信3g
                        TelephonyManager.NETWORK_TYPE_UMTS,
                        TelephonyManager.NETWORK_TYPE_EVDO_0,
                        TelephonyManager.NETWORK_TYPE_HSDPA,
                        TelephonyManager.NETWORK_TYPE_HSUPA,
                        TelephonyManager.NETWORK_TYPE_HSPA,
                        TelephonyManager.NETWORK_TYPE_EVDO_B,
                        TelephonyManager.NETWORK_TYPE_EHRPD,
                        TelephonyManager.NETWORK_TYPE_HSPAP -> return NETWORK_3G
                    //如果是4g类型
                        TelephonyManager.NETWORK_TYPE_LTE -> return NETWORK_4G
                        else ->
                            //中国移动 联通 电信 三种3G制式
                            if (strSubTypeName.equals("TD-SCDMA", ignoreCase = true) ||
                                    strSubTypeName.equals("WCDMA", ignoreCase = true) ||
                                    strSubTypeName.equals("CDMA2000", ignoreCase = true)) {
                                return NETWORK_3G
                            } else {
                                return NETWORK_MOBILE
                            }
                    }
                }
        }
        return NETWORK_NONE
    }

}